package com.myorg.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.myorg.domain.SearchResult;
import com.myorg.service.SearchService;
import com.myorg.service.StartupService;

@WebServlet("/DoSearch")
public class DoSearch extends HttpServlet {
	private static final long serialVersionUID = 1L;

	final Logger logger = LoggerFactory.getLogger(DoSearch.class);

	public DoSearch() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		StartupService.touch();
		String source = request.getParameter("source");
		String startS = request.getParameter("start");
		String endS = request.getParameter("end");
		String key = request.getParameter("key");
		String compress = request.getParameter("compress");
		int max = Integer.parseInt(request.getParameter("max"));
		String[] ss = request.getParameterValues("ignoreId");
		List<String> ignoreIdList = new ArrayList<String>();
		if (ss != null) {
			for (String s : ss) {
				ignoreIdList.add(s);
			}
		}
		SearchResult rtn = new SearchService().doSearch(source, startS, endS, key, max, ignoreIdList,
				"Y".equalsIgnoreCase(compress));
		String outputString = new Gson().toJson(rtn);
		logger.info("Json output(node): " + outputString);
		response.getWriter().write(outputString);
	}
}
